package com.sunday.shangjia.http;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.PushConsts;
import com.sunday.common.logger.Logger;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.member.common.MemberConst;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.shangjia.R;
import com.sunday.shangjia.entity.PushModel;
import com.sunday.shangjia.ui.MainActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/1/19.
 */

public class PushReceiver extends BroadcastReceiver {


    protected static int notifyID = 0525; // start notification id
    String memberId,shopId;
    private Gson gson = new Gson();

    @Override
    public void onReceive(Context context, Intent intent) {
       Bundle bundle = intent.getExtras();
        Logger.d("GetuiSdkDemo", "onReceive() action=" + bundle.getInt("action"));
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_CLIENTID:
                String cid = bundle.getString("clientid");
                SharePerferenceUtils.getIns(context).putString("clientId", cid);
                boolean isLogin = SharePerferenceUtils.getIns(context).getBoolean(MemberConst.IS_LOGIN, false);
                if (isLogin) {
                    memberId = SharePerferenceUtils.getIns(context).getString(Constants.MEMBERID, "");
                    shopId=SharePerferenceUtils.getIns(context).getString("shopId","");
                }
                Call<ResultDO> call = AppClient.getAppAdapter().uploadToken(shopId,memberId, cid, 1);
                call.enqueue(new Callback<ResultDO>() {
                    @Override
                    public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                        if (response.body() == null) {
                            return;
                        }
                        if (response.body().getCode() == 0) {

                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO> call, Throwable t) {

                    }
                });
                break;
            case PushConsts.GET_MSG_DATA:

                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");
                byte[] payload = bundle.getByteArray("payload");
                if (payload != null) {
                    String data = new String(payload);
                    // TODO:接收处理透传（payload）数据
                    showNotify(context,data,null,null);
                }
                break;
            default:
                break;
        }

    }

    private void showNotify(Context context, String content, Uri uri, String code) {

       NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        String packageName = context.getApplicationInfo().packageName;
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent, 0);
        mBuilder.setContentTitle("天奢商家版")//设置通知栏标题
                .setContentText(content)
                .setContentIntent(pendingIntent) //设置通知栏点击意图
//	.                   setNumber(number) //设置通知集合的数量
                .setTicker("天奢商家") //通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                //.setDefaults(open?Notification.STREAM_DEFAULT:Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND  DEFAULT_VIBRATE 添加声音 // requires VIBRATE permission
                .setDefaults(Notification.DEFAULT_ALL)
                // .setSound(Uri.parse("file:///sdcard/order.mp3"))
                // .setSound(Uri.parse(context.getResources().openRawResource(R.raw.order))
                .setSmallIcon(R.mipmap.logo);//设置通知小ICON
        mNotificationManager.notify(notifyID, mBuilder.build());

    }


}
